-
Notifications
You must be signed in to change notification settings - Fork 784
feat: add agent span support in langchain instrumentation #3788
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
elif "id" in serialized: | ||
chain_name = serialized["id"][-1] | ||
|
||
span = self.span_manager.create_chain_span( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's make sure it creates internal span
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sound good - confirmed chain spans use SpanKind.INTERNAL
here:
def create_chain_span(
self,
run_id: UUID,
parent_run_id: Optional[UUID],
chain_name: str,
) -> Span:
"""Create a span for chain execution."""
span = self._create_span(
run_id=run_id,
parent_run_id=parent_run_id,
span_name=f"chain {chain_name}",
kind=SpanKind.INTERNAL,
)
) | ||
span.set_attribute( | ||
GenAI.GEN_AI_OPERATION_NAME, | ||
"invoke_agent", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible that the operation name might be changed in the future, if so, declaring it a constant for it might be better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point - I'm not sure if this semantic convention will change but agree that this should be moved to a constant since it's currently hardcoded in a few places.
GenAI.GEN_AI_OPERATION_NAME, | ||
"invoke_agent", | ||
) | ||
if agent_name: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If agent_name is None, in that case, should a default value be passed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah makes sense, i'll default it to "unknown" for now. Open to suggestions on the naming though.
span = spans[0] | ||
assert span.name == "chain TestAgent" | ||
assert span.kind == SpanKind.INTERNAL | ||
assert span.attributes.get(GenAI.GEN_AI_AGENT_NAME) == "TestAgent" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the above comment you mentioned that chain spans are internal operations and will not have an operation name but there you are adding assert statements for it, did I miss something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your review! Let me try to clarify.
Regular chains don't have gen_ai.operation.name
since they're just internal operations. However, Agent chains are different. In LangChain's architecture, agents are implemented as chains so we:
- create them using
create_agent_span()
(which creates an internal span) - detect if it's actually an agent by checking for
agent_name
in metadata - if it is an agent, we add the agent-specific attributes:
gen_ai.agent.name
gen_ai.operation.name = "invoke_agent"
I'll update the docstrings to make this distinction clearer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome. Thanks for the clarification.
Description
This PR adds agent span support to the LangChain instrumentation following the OpenTelemetry Semantic Conventions for GenAI agent spans. It enables tracing of LangChain agent invocations, chain executions, and agent actions/decisions.
invoke_agent
operation for agent invocationsinvoke_agent {agent_name}
andchain {chain_name}
gen_ai.agent.name
,gen_ai.operation.name
A follow-up PR will add tool execution spans (
execute_tool
operation).Sidenote: Previous PR from our team can be closed.
Type of change
Please delete options that are not relevant.
How Has This Been Tested?
Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration
relationships
Does This PR Require a Core Repo Change?
Checklist:
See contributing.md for styleguide, changelog guidelines, and more.